/*global JSLINT:false */

(function () {
    'use strict';

    function fileName(url) {
        return url.match(/([^\/]*)$/)[1];
    }

    function onXHRLoad() {
        var data, err, i, url, predef = [];

        url = this.responseURL || this.requestURL;
        if (!url.match(/lint\.js$/)) {
            predef.push('BROWSER');
            predef.push('NOOGL');
            predef.push('Matrix');
        }
        if (url.match(/_tests\.js$/)) {
            predef.push('BDD');
        }
        JSLINT(this.responseText, {
            browser: true,
            devel: (url.match(/\/bdd\.js$/) || url.match(/\/lint\.js$/)),
            predef: predef,
            regexp: (url.match(/\/lint\.js$/) || url.match(/_tests\.js$/)),
            typeofeqobj: url.match(/\/bdd\.js$/),
            unparam: true
        });
        data = JSLINT.data();
        if (data.errors.length === 0) {
            return;
        }

        console.groupCollapsed('\u2717 ' + 'jslint errors ' + fileName(url) + ' ' + data.errors.length + ' errors');
        for (i = 0; i < data.errors.length; i += 1) {
            err = data.errors[i];
            if (err) {
                console.log(url + ':' + err.line + ':' + err.character, err.reason, err.evidence);
            }
        }
        console.groupEnd();

    }
    var scripts = document.scripts, s, script, xhr;
    for (s = 0; s < scripts.length; s += 1) {
        script = scripts[s];
        if (script.src && script.src.match(/\.js$/)) {
            xhr = new XMLHttpRequest();
            xhr.onload = onXHRLoad;
            xhr.open('GET', script.src);
            xhr.requestURL = xhr.requestURL || script.src;
            xhr.send();
        }
    }
}());
